package com.aqie.appletserver.common.response;

import java.io.Serializable;

/**
 * @Function:
 * @Author: aqie
 * @Date: 2019/4/5 11:44
 */
public class ResponseModel<M> implements Serializable {
    private int code;
    private String message;

    private M data;

    public ResponseModel() {
        code = 1;
        message = "ok";
    }

    public ResponseModel(String msg){
        code = 0;
        message = msg;
    }

    public ResponseModel(M result) {
        this();
        this.data = result;
    }

    public static <M> ResponseModel<M> success() {
        return new ResponseModel<M>();
    }


    // 成功时调用
    public static <M> ResponseModel<M> success(M data) {
        return new ResponseModel<M>(data);
    }

    public static <M> ResponseModel<M> error(String msg){
        return new ResponseModel<>(msg);
    }
    public static <M> ResponseModel<M> error(CodeMsg codeMsg){
        return new ResponseModel<M>(codeMsg);
    }

    private ResponseModel(CodeMsg codeMsg){
        if (codeMsg == null)
            return;
        this.code = codeMsg.getCode();
        this.message = codeMsg.getMsg();
    }


    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public M getData() {
        return data;
    }

    public void setData(M data) {
        this.data = data;
    }
}
